// HSS.Net.Mail.BufferBuilder.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       BufferBuilder.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Net.Mail
{
	#region Using Directives
	using System;
	#endregion

	#region BufferBuilder
	internal class BufferBuilder
	{
		int initSize;
		internal BufferBuilder()
			: this(0x100)
		{
		}

		internal BufferBuilder(int initialSize)
		{
			initSize = initialSize;
			this.buffer = new byte[initSize];
		}

		internal void Append(byte[] value)
		{
			this.Append(value, 0, value.Length);
		}

		internal void Append(byte value)
		{
			this.EnsureBuffer(1);
			this.buffer[this.offset++] = value;
		}

		internal void Append(string value)
		{
			this.Append(value, 0, value.Length);
		}

		internal void Append(byte[] value, int offset, int count)
		{
			this.EnsureBuffer(count);
			Buffer.BlockCopy(value, offset, this.buffer, this.offset, count);
			this.offset += count;
		}

		internal void Append(string value, int offset, int count)
		{
			this.EnsureBuffer(count);
			for (int num1 = 0; num1 < count; num1++)
			{
				char ch1 = value[offset + num1];
				if (ch1 > '\x00ff')
				{
					throw new FormatException(HSS.Net.Mail.Resources.MailHeaderFieldInvalidCharacter);
				}
				this.buffer[this.offset + num1] = (byte)ch1;
			}
			this.offset += count;
		}

		private void EnsureBuffer(int count)
		{
			if (count > (this.buffer.Length - this.offset))
			{
				byte[] buffer1 = new byte[((this.buffer.Length * 2) > (this.buffer.Length + count)) ? (this.buffer.Length * 2) : (this.buffer.Length + count)];
				Buffer.BlockCopy(this.buffer, 0, buffer1, 0, this.offset);
				this.buffer = buffer1;
			}
		}

		internal byte[] GetBuffer()
		{
			return this.buffer;
		}

		internal void Reset()
		{
			this.offset = 0;
		}

		internal void Clear()
		{
			this.offset = 0;
			this.buffer = new byte[initSize];
		}

		internal int Length
		{
			get
			{
				return this.offset;
			}
		}

		public override string ToString()
		{
			return System.Text.Encoding.ASCII.GetString(this.buffer);
		}

		private byte[] buffer;
		private int offset;
	}
	#endregion
}